//
//  ZLAppData.m
//  ZL_HOUSEWORK
//
//  Created by mxf on 16/6/21.
//  Copyright © 2016年 ZL. All rights reserved.
//

#import "ZLAppData.h"

@implementation ZLAppData
+(instancetype)shareAppData
{
    static ZLAppData *appData = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        appData = [[self alloc] init];
    });
    return appData;
}

-(void)saveUser:(UserModel *)user
{
    if (user) {
        NSString *userModelTojson = [ZLHelp objToJson:user];
        [[NSUserDefaults standardUserDefaults] setObject:userModelTojson forKey:@"USERMODEL"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }else{
        NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
        NSDictionary * dict = [defs dictionaryRepresentation];
        for (id key in dict) {
            if ([key isEqualToString:@"USERMODEL"]) {
                [defs removeObjectForKey:key];
                break;
            }
        }
        [defs synchronize];
    }
}

-(UserModel *)getUserModel
{
   NSString * userModelTojson =[[NSUserDefaults standardUserDefaults] objectForKey:@"USERMODEL"];
    if (userModelTojson == nil) {
        return nil;
    }else{
        NSDictionary *dic =  [ZLHelp jsonToObj:userModelTojson];
        UserModel *user = [[UserModel alloc] init];
        user.user_id = [dic objectForKey:@"user_id"];
        return user;
    }
    
}

@end
